<?php 
    include("header.php");
    if ( isset($_SESSION['user']) == false || $_SESSION['user']->can_upload == false  ) {
        redirect("/tornooien/tornooien.php");
    }
    $user = $_SESSION['user'];
?>

<head>
    <title>Liga - Upload</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link href="/assets/css/default.css" rel="stylesheet" type="text/css">
    
    <style type="text/css">
        @import "/assets/js/dojo-release-1.1.0/dijit/themes/nihilo/nihilo.css";
        
        @import "/assets/js/dojo-release-1.1.0/dojox/highlight/resources/highlight.css";
        @import "/assets/js/dojo-release-1.1.0/dojox/highlight/resources/pygments/default.css";
        @import "/assets/js/dojo-release-1.1.0/dojo/resources/dojo.css"
        
        @import "/assets/js/dojo-release-1.1.0/dojox/grid/_grid/Grid.css";
        @import "/assets/js/dojo-release-1.1.0/dojox/grid/_grid/nihiloGrid.css";
    </style>
    
    <script type="text/javascript" src="/assets/js/dojo-release-1.1.0/dojo/dojo.js"
      djConfig="parseOnLoad: true"></script>
      
    <script type="text/javascript">
        dojo.require("dojo.io.iframe"); 
        dojo.require("dijit.ProgressBar"); 
        dojo.require("dijit.form.FilteringSelect");
        dojo.require("dijit.form.ComboBox");
        dojo.require("dijit.form.DateTextBox");
        dojo.require("dijit.InlineEditBox");
        dojo.require("dijit.form.Button");
        dojo.require("dijit.form.TextBox");
        dojo.require("dojo.parser");
        
        // it's all wrapped up in a single function
        function sendForm(){
          
          dojo.byId('ref').value = dojo.byId('refInlineBox').value;
          
          //Hide the file input field
          dojo.style('inputField',"display","none");
          
          //Show the progress bar
          dojo.style('progressField',"display","inline"); 
          dojo.byId('preamble').className='default';
          dojo.byId('preamble').innerHTML = "Uploading ..."; 

          dojo.io.iframe.send({
            url: "file_upload_script.php",
            method: "post",
            handleAs: "text",
            form: dojo.byId('mainForm'),
            handle: function(data,ioArgs){
              var foo = dojo.fromJson(data);
              if (foo.status == "success"){
                //Show the file input field
                dojo.style(dojo.byId('inputField'),"display","inline");
                dojo.byId('fileInput').value = '';
                
                //Hide the progress bar
                dojo.style(dojo.byId('progressField'),"display","none"); 
                var currentTime = new Date();
                var hours = currentTime.getHours();
                var minutes = currentTime.getMinutes();
                var seconds = currentTime.getSeconds();

                if (foo.details.name == '') {
                    dojo.byId('uploadedFiles').innerHTML += hours + ":" + minutes + ":" + seconds 
                        + "  OK, gegevens gewijzigd<br>"; 
                } else {
                    dojo.byId('uploadedFiles').innerHTML += hours + ":" + minutes + ":" + seconds 
                        + "  OK, bestand opgeladen: " + foo.details.name 
                        + " size: " + foo.details.size +"<br>"; 
                }
                
                dojo.byId('preamble').className='default';
                dojo.byId('preamble').innerHTML = "Bestand om op te laden: ";       
              }else{
                dojo.style(dojo.byId('inputField'),"display","inline");
                dojo.style(dojo.byId('progressField'),"display","none"); 
                dojo.byId('preamble').className='error';
                dojo.byId('preamble').innerHTML = "Fout: " + foo.error + " Probeer opnieuw."; 
              }  
            }
          });
        }
        
        function setValue(value) {
            dojo.byId('refInlineBox').value=arguments[0];
            dojo.byId('tournamentName').value=dojo.byId('tournament').value;
        }
    </script>

</head>

<?
    setTabIndex(2);
    createBody("default");
?>

<?
    mysql_connect(DB_SERVER, DB_USER, DB_PWD);
    mysql_select_db(DB_CATALOG);
    $result = mysql_query("select upload_id, ref, name from uploaded_tournament where user_id = ".$user->user_id." order by name ASC");

    $uploadedTournaments = array();
    while ($row = mysql_fetch_assoc($result)) {
        $uploadedTournament = new UploadedTournament();
        $uploadedTournament->upload_id = $row['upload_id'];
        $uploadedTournament->ref = $row['ref'];
        $uploadedTournament->name = $row['name'];
        array_push($uploadedTournaments, $uploadedTournament);
    }
?>

<table height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0" bgcolor="#FFFFFF">

    <tr>
        <td id="leftFiller" valign="top" height="100%" rowspan="3" bgcolor="#666666">
            <table border="0" cellpadding="0" cellspacing="0" height="100%" width="8px">
                <tr><td>&nbsp;</td></tr>
            </table>
        </td>
        <td id="header" valign="top" width="100%">
            <table border="0" width="100%" cellpadding="3" cellspacing="0" bgcolor="#10425b">
                <tr>
                    <td bgcolor="#666666" width="70%">
                        <?php include("inc_title.html"); ?>
                    </td>
                    <td bgcolor="#666666" align="right" valign="top">&nbsp;

                    </td>
                </tr>
            </table>
            <table align="center" border="0" width="100%" height="90%" cellpadding="0" cellspacing="0" style="border-top:#BCDD10 4px solid;">
                <tr>
                    <?php include("inc_sublinks.php"); ?>   
                </tr>
                <tr><td colspan="2" nowrap bgcolor="#BCDD10" height="4"><img src="/assets/images/spacer.gif" alt="" height="1" width="1"></td></tr>
                <tr>
                    <td>&nbsp;</td>
                    <td valign="top">
                        <table align="left" cellspacing="10" width="90%">
                            <tr>
                                <td>
                                    <span class="blackHeaderLarge">Upload</span>
                                    <br>
                                    <form id="mainForm" 
                                      enctype="multipart/form-data" 
                                      name="fileTest" 
                                      action="file_upload_script.php" 
                                      method="post">
                                      
                                      <br/>
                                      <!-- no real reason to have a div around this part -->
                                      <div id="uploadContainer" class="nihilo">
                                        <input type="hidden" name="MAX_FILE_SIZE" value="500000">
                                        <!-- wrapping these in spans to be able to modify 
                                          parts of this form depending on what the 
                                          dojo.io.iframe.submit() does -->
                                      
                                        <table align="left" cellspacing="5" cellpadding="2" width="90%">
                                            <tr>
                                                <td><strong>Naam tornooi</strong></td>
                                                <td>                                                    
                                                    <select dojoType="dijit.form.FilteringSelect"
                                                      id="tournament"
                                                      name="tournament"
                                                      autoComplete="false"
                                                      onchange="setValue"
                                                      invalidMessage="&nbsp;Nieuw tornooi&nbsp;">
                                                      
                                                        <option value="?"></option>
                                                    <?
                                                    foreach ($uploadedTournaments as $item) {
                                                    ?>
                                                        <option value="<?=$item->ref?>"><?=$item->name?></option>
                                                    <?
                                                    }
                                                    ?>
                                                    </select>
                                                    
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2">
                                                Wanneer de gebruiker op de naam van het tornooi klikt wordt hij doorverwezen naar
                                                <strong>
                                                <!--<label id="refInlineBox" name="refInlineBox" dojoType="dijit.InlineEditBox" 
                                                    title="Voer hier uw referentiepagina in" autoSave="true" width="150">?</label>-->
                                                <input id="refInlineBox" name="refInlineBox" style="width: 200px;" autocomplete="off"/>
                                                </strong>
                                                (bv. rapid.html)
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2">&nbsp;</td>
                                            </tr>
                                            <tr>
                                                <td colspan="2">
                                                    <strong>(Optioneel)</strong>
                                                    <span id="preamble">Bestand om op te laden:</span><br>
                                                    <span id="inputField">
                                                      <input type="file" id="fileInput" name="uploadTestFile">
                                                    </span>
                                                  
                                                    <span id="progressField" style="display:none;">
                                                      <div dojoType="dijit.ProgressBar" style="width:200px" 
                                                        indeterminate="true"></div>
                                                    </span>
                                                </td>
                                            </tr>
                                            <input type="hidden" id="ref" name="ref" /> 
                                            <input type="hidden" id="tournamentName" name="tournamentName" /> 
                                            <tr>
                                                <td colspan="2">
                                                    <button class="nihilo" value="upload" dojoType="dijit.form.Button" 
                                                        onclick="sendForm()">Doorsturen</button>
                                                </td>
                                            </tr>
                                            
                                            </form>
                                            <tr>
                                                <td colspan="2">
                                                    <div id="uploadedFiles" class="nihilo">
                                                      <span class="blackHeaderLarge">Historiek</span>  
                                                      <br>
                                                    </div>
                                                </td>
                                            </tr>
                                        </table>
                                        <br>
                                      </div>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

<?php include("footer.php") ?>